#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
struct Node {
  int pri, val;
  int ch[2];
  int sz;
} tr[N];
int rt;
int newnode(int v) {
  static int tot = 0;
  ++tot;
  tr[tot].pri = rand();
  tr[tot].val = v;
  tr[tot].sz = 1;
  tr[tot].ch[0] = tr[tot].ch[1] = 0;
  return tot;
}
void pushup(int p) { tr[p].sz = tr[tr[p].ch[0]].sz + tr[tr[p].ch[1]].sz + 1; }
void split(int p, int &l, int &r, int v) {
  if (!p) {
    l = r = 0;
    return;
  }
  if (tr[p].val <= v) {
    l = p;
    split(tr[p].ch[1], tr[p].ch[1], r, v);
    pushup(l);
  } else {
    r = p;
    split(tr[p].ch[0], l, tr[p].ch[0], v);
    pushup(r);
  }
}
int merge(int l, int r) {
  if (!l || !r) return l + r;
  if (tr[l].pri >= tr[r].pri) {
    tr[l].ch[1] = merge(tr[l].ch[1], r);
    pushup(l);
    return l;
  } else {
    tr[r].ch[0] = merge(l, tr[r].ch[0]);
    pushup(r);
    return r;
  }
}
void insert(int v) {
  int l, r;
  split(rt, l, r, v);
  int m = newnode(v);
  rt = merge(merge(l, m), r);
}
void del(int v) {
  int l, m, r;
  split(rt, l, r, v);
  split(l, l, m, v - 1);
  m = merge(tr[m].ch[0], tr[m].ch[1]);
  rt = merge(merge(l, m), r);
}
int get_rank(int v) {
  int l, r;
  split(rt, l, r, v - 1);
  int res = tr[l].sz + 1;
  rt = merge(l, r);
  return res;
}
int val(int p, int k) {
  if (tr[tr[p].ch[0]].sz >= k)
    return val(tr[p].ch[0], k);
  else if (tr[tr[p].ch[0]].sz + 1 == k)
    return tr[p].val;
  else
    return val(tr[p].ch[1], k - tr[tr[p].ch[0]].sz - 1);
}
int pre(int v) {
  int l, r;
  split(rt, l, r, v - 1);
  int res = val(l, tr[l].sz);
  rt = merge(l, r);
  return res;
}
int nxt(int v) {
  int l, r;
  split(rt, l, r, v);
  int res = val(r, 1);
  rt = merge(l, r);
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int op, x;
    cin >> op >> x;
    if (op == 1) insert(x);
    if (op == 2) del(x);
    if (op == 3) cout << get_rank(x) << endl;
    if (op == 4) cout << val(rt, x) << endl;
    if (op == 5) cout << pre(x) << endl;
    if (op == 6) cout << nxt(x) << endl;
  }
  return 0;
}